package com.windy.dailynews.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.windy.dailynews.R;
import com.windy.dailynews.model.Summary;
import com.windy.dailynews.presenter.ThemeNewsContract;
import com.windy.dailynews.presenter.impl.ThemeNewsPresenter;
import com.windy.dailynews.ui.activity.NewsDetailActivity;
import com.windy.dailynews.ui.adapter.NewsListAdapter;
import com.windy.xlistview.XListView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by dingfeng on 2016/11/22.
 */
public class ThemeNewsFragment extends Fragment implements ThemeNewsContract.View {

    private Context mContext;
    private ThemeNewsContract.Presenter mPresenter;
    private int id;
    private NewsListAdapter mListAdapter;
    private List<Summary> mSummaryList;
    private ImageView topImage;

    @Bind(R.id.listView)
    XListView mListView;
    @Bind(R.id.loading)
    View loading;
    @Bind(R.id.load_failed)
    View load_failed;

    public static ThemeNewsFragment newInstance() {
        return new ThemeNewsFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_theme_news, container, false);
        ButterKnife.bind(this, view);
        mContext = getActivity();
        id = getArguments().getInt("id", 0);
        initView();
        return view;
    }

    private void initView() {
        load_failed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.loadData(id);
            }
        });
        View header = LayoutInflater.from(mContext).inflate(R.layout.include_top_image, null);
        topImage = (ImageView) header.findViewById(R.id.topImage);
        mListView.addHeaderView(header);
        initListView();
        new ThemeNewsPresenter(this);
        mPresenter.loadData(id);
    }

    private void initListView() {
        mListView.setOnItemClickListener(onItemClickListener);
        mListView.setPullLoadEnable(false);
        mListView.setPullRefreshEnable(false);
    }

    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int realPos = position - 2;
            if (realPos > -1) {
                Intent intent = new Intent(mContext, NewsDetailActivity.class);
                int newsId = mSummaryList.get(realPos).getId();
                intent.putExtra("newsId", newsId);
                startActivity(intent);
            }
        }
    };

    @Override
    public void showCoverImage(String url) {
        Glide.with(mContext.getApplicationContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(topImage);
    }

    @Override
    public void showNewsList(List<Summary> list) {
        mSummaryList = list;
        mListAdapter = new NewsListAdapter(mContext, mSummaryList);
        mListView.setAdapter(mListAdapter);
    }

    @Override
    public void setLoading() {
        loading.setVisibility(View.VISIBLE);
        load_failed.setVisibility(View.GONE);
    }

    @Override
    public void hideLoading() {
        loading.setVisibility(View.GONE);
        load_failed.setVisibility(View.GONE);
    }

    @Override
    public void setError() {
        loading.setVisibility(View.GONE);
        load_failed.setVisibility(View.VISIBLE);
    }

    @Override
    public void setPresenter(ThemeNewsContract.Presenter presenter) {
        mPresenter = presenter;
    }

}
